package at.fhj.swd.pse.data;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import at.fhj.swd.pse.JpaTestBase;
import at.fhj.swd.pse.entities.Activity;
import at.fhj.swd.pse.entities.EActivityType;

public class UTActivityDAO extends JpaTestBase {
	private final EntityManager m_em = this.getEntityManagerForPersistenceUnit("test");
	private ActivityDAO aDao;

	@Before
	public void setUp() {
		this.aDao = new ActivityDAOImpl(this.m_em);
		this.begin();
	}

	@After
	public void tearDown() {
		this.rollback();
		// commit();
		this.closeEntityManager();
	}

	@Test
	public void testAddActivity() {
		Activity activity = this.newTestActivity();
		activity = this.aDao.insert(activity);
		List<Activity> activities = this.aDao.getAll();
		assertTrue(activities.size() > 0);
		Activity testActivity = this.aDao.get(activity.getId());
		assertEquals(activity.getCreator(), testActivity.getCreator());
		assertEquals(activity.getText(), testActivity.getText());
		assertEquals(activity.getTimestamp(), testActivity.getTimestamp());
		assertEquals(activity.getType(), testActivity.getType());
	}

	@Test
	public void testRemoveActivity() {
		Activity activity = this.newTestActivity();
		activity = this.aDao.insert(activity);
		Activity testActivity = this.aDao.get(activity.getId());
		assertNotNull(testActivity);
		this.aDao.remove(testActivity);
		assertNull(this.aDao.get(activity.getId()));
	}

	private Activity newTestActivity() {
		Activity activity = new Activity();
		activity.setText("Hello Test");
		activity.setType(EActivityType.Global);
		activity.setTimestamp(new Date(System.currentTimeMillis()));
		return activity;
	}
}
